#include "View.h"
#include <vector>

class SubviewManager
{
private:
	std::vector<View*> _subviews;
public:
	~SubviewManager()
	{
		for (auto v : _subviews)
		{
			delete v;
		}
		_subviews.clear();
	}

	void Draw()
	{
		for (auto v : _subviews)
		{
			v->Draw();
		}
	}

	void Add(View* view)
	{
		_subviews.push_back(view);
	}

	void Remove(View* view)
	{
		std::erase(_subviews, view);
		delete view;
	}
};

//------------------------------------------------------------

void View::Draw()
{
	_subviewManager->Draw();
}

void View::AddSubView(View* view)
{
	_subviewManager->Add(view);
}

void View::RemoveSubView(View* view)
{
	_subviewManager->Remove(view);
}

View::View()
{
	_subviewManager = new SubviewManager();
}

View::View(View&& other) noexcept
	:_subviewManager(other._subviewManager)
{
	other._subviewManager = nullptr;
}

View::~View()
{
	delete _subviewManager;
}

